package com.qinggeng.common.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 时间工具类
 *
 * @author qinggeng
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static boolean thanHHmm(String begin,String end){
        String[] beginSplit = begin.split(":");
        int beginTime = Integer.valueOf(beginSplit[0]) * 3600 + Integer.valueOf(beginSplit[1]) * 60;
        String[] endSplit = end.split(":");
        int endTime = Integer.valueOf(endSplit[0]) * 3600 + Integer.valueOf(endSplit[1]) * 60;
        if (beginTime >= endTime){
            return true;
        }
        return false;
    }

    /**
     * 获取当前周的周一的日期
     * @param date 传入当前日期
     * @return
     */
    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    /**
     * 使用方法·传入String类型
     * @param dt
     * @return
     */
    public static String getTime(String dt) {
        //拆分传来的日期，获取详细数值
        int year = 0;//年份
        int month = 0;//月份
        int day = 0;//日
        int hour = 0;//小时
        int minute = 0;//分
        int second = 0;//秒
        String str[] = dt.split(" ");
        for (int i = 0; i < str.length; i++) {
            String str1[] = str[0].split("-");
            for (int j = 0; j < str1.length; j++) {
                year = Integer.valueOf(str1[0]);
                month = Integer.valueOf(str1[1]);
                day = Integer.valueOf(str1[2]);
            }
            String str2[] = str[1].split(":");
            for (int j = 0; j < str2.length; j++) {
                hour = Integer.valueOf(str2[0]);
                minute = Integer.valueOf(str2[1]);
                second = Integer.valueOf(str2[2]);
            }
            break;
        }
        //获取当前时间
        Calendar cal = Calendar.getInstance();
        int years = cal.get(Calendar.YEAR);//获取年份
        int months = cal.get(Calendar.MONTH) + 1;//获取月份 ，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
        int days = cal.get(Calendar.DATE);//获取日
        int hours = cal.get(Calendar.HOUR_OF_DAY);//小时  //Calendar.HOUR是12小时制
        int minutes = cal.get(Calendar.MINUTE);//分
        int seconds = cal.get(Calendar.SECOND);//秒
        int WeekOfYears = cal.get(Calendar.DAY_OF_WEEK);//一周的第几天，英语国家星期从星期日开始计算


        //比较是不是今天
        if (year == years && month == months && day == days) {
            //如果是今天，验证是不是同一个小时
            if (hour == hours) {
                //验证是不是同一分钟
                if (minute == minutes || (minutes-minute) == 1) {
                    //是同一分钟，返回刚刚
                    return "刚刚";
                }else{
                    return (minutes-minute) + "分钟前";
                }
            }else{
                //验证时区
                if (hour <= 6) {//7点之前
                    return "凌晨 "+ hour + ":" + minute;
                }
                if (hour > 6 && hour <= 8) {//7-9点
                    return "早上 "+ hour + ":" + minute;
                }
                if (hour > 8 && hour <= 11) {//9-12点
                    return "上午 "+ hour + ":" + minute;
                }
                if (hour > 11 && hour <= 12) {//12点
                    return "中午 "+ hour + ":" + minute;
                }
                if (hour > 12 && hour <= 17) {//14-18点
                    return "下午 "+ hour + ":" + minute;
                }
                if (hour > 17 && hour <= 19) {//19-20点
                    return "傍晚 "+ hour + ":" + minute;
                }
                if (hour > 19 ) {//20以后
                    return "晚上 "+ hour + ":" + minute;
                }
            }
            //比较是不是昨天
        }else if (year == years && month == months && (days-day) == 1) {
            return "昨天";
            //比较是不是今年
        }else if (year == years) {
            return month+"月"+day+"日";
        }else{
            return year+"/"+month+"/"+day;
        }
        return " ";
    }

    public static String getAutoTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Calendar currentCalendar = Calendar.getInstance();
        currentCalendar.set(Calendar.HOUR_OF_DAY, 0);
        currentCalendar.set(Calendar.MINUTE, 0);
        currentCalendar.set(Calendar.SECOND, 0);
        currentCalendar.set(Calendar.MILLISECOND, 0);
        long dateL = calendar.getTimeInMillis();
        long nowL = currentCalendar.getTimeInMillis();
        if (dateL < nowL - 1296000000L) { // 比15天前还早
            if (calendar.get(Calendar.YEAR) == currentCalendar.get(Calendar.YEAR)) { // 在今年
                return getDate(date);
            } else {
                return getDateOneYear(date);
            }
        } else if (dateL < nowL - 172800000L) { // 比今天0时还早48小时以上
            return ((nowL - dateL) / 86400000L) + "天前";
        } else if (dateL < nowL - 86400000L) { // 比今天0时还早24小时以上
            return "前天 " + getTime(date);
        } else if (dateL < nowL) { // 比今天0时还早
            return "昨天 " + getTime(date);
        } else if (dateL < nowL + 21600000L) { // 今天6点前
            return "凌晨 " + getTime(date);
        } else if (dateL < nowL + 43200000L) { // 今天12点前
            return "早上 " + getTime(date);
        } else if (dateL < nowL + 64800000L) { // 今天18点前
            return "下午 " + getTime(date);
        } else if (dateL < nowL + 86400000L) { // 明天0时前
            return "晚上 " + getTime(date);
        } else if (dateL < nowL + 172800000L) { // 晚今天0时48小时内
            return "明天 " + getTime(date);
        } else if (dateL < nowL + 259200000L) { // 晚今天0时72小时内
            return "后天 " + getTime(date);
        } else {
            return getDateOneYear(date);
        }
    }
    public static String getBaseDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:00", Locale.getDefault());
        return sdf.format(date);
    }
    public static String getDateOneYear(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault());
        return sdf.format(date);
    }

    public static String getDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日", Locale.getDefault());
        return sdf.format(date);
    }

    public static String getTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
        return sdf.format(date);
    }

    public static String getWeek(int value) {
        switch (value) {
            case 1:
                return "周日";
            case 2:
                return "周一";
            case 3:
                return "周二";
            case 4:
                return "周三";
            case 5:
                return "周四";
            case 6:
                return "周五";
            default:
                return "周六";
        }
    }

    //获取今天剩下时间秒数
    public static long getSeconds(){
        Calendar curDate = Calendar.getInstance();
        Calendar tommorowDate = new GregorianCalendar(curDate
                .get(Calendar.YEAR), curDate.get(Calendar.MONTH), curDate
                .get(Calendar.DATE) + 1, 0, 0, 0);
        return (tommorowDate.getTimeInMillis() - curDate .getTimeInMillis()) / 1000;
    }

    /**
     * 获取过去第几天的日期
     * @param past
     * @return String
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
        String result = format.format(today);
        return result;
    }

    public static void main(String[] args) {
        //查询所有配送区域自提点一天的商品销量统计
        //昨天
        String pastDate = DateUtils.getPastDate(1);
        //今天
        String date = DateUtils.getDate();
        //具体时间
        String time = "22:00:00";
        String beforeTime = pastDate + " " + time;
        String afterTime = date + " " + time;

        System.out.println(beforeTime +"----"+afterTime);
    }
}
